package a20.com.yuejian_20.activity;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import a20.com.yuejian_20.R;
import a20.com.yuejian_20.fragments.ClubFragment;
import a20.com.yuejian_20.fragments.CourseFragment;
import a20.com.yuejian_20.fragments.RecomFragment;
import a20.com.yuejian_20.fragments.SettingFragment;
import a20.com.yuejian_20.fragments.ShareFragment;

/**
 * Created by ASURA on 2017/9/25.
 */

public class MainActivity extends FragmentActivity{
    private RadioGroup select;
    private Fragment fragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        initView();
        initEvent();
        select.check(R.id.course);
    }

    private void initView(){
        select = (RadioGroup)findViewById(R.id.select);
    }

    private void setFragment(){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if(fragment == null){
            fragment = new CourseFragment();
        }
        ft.replace(R.id.frame,fragment);
        ft.commit();
    }

    private void initEvent(){
        select.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch(checkedId){
                    case R.id.course :
                        fragment = new CourseFragment();
                        break;
                    case R.id.club:
                        fragment = new ClubFragment();
                        break;
                    case R.id.recom:
                        fragment = new RecomFragment();
                        break;
                    case R.id.share:
                        fragment = new ShareFragment();
                        break;
                    case R.id.setting:
                        fragment = new SettingFragment();
                        break;
                    default:
                        fragment = new CourseFragment();
                        break;
                }
                setFragment();
            }
        });

    }
}
